package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体检套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;

    //新增套餐信息，同时需要关联检查组
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
        //将图片名称保存到Redis集合中
        String fileName = setmeal.getImg();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 查询所有套餐,用redis实现存储
    * */
    public List<Setmeal> findAll() {
        //从redis中获取所有套餐数据，json格式
        String allSetmeal = jedisPool.getResource().get("allSetmeal");
        //如果redis中没有套餐数据则进行数据库查询
        if(allSetmeal == null || "".equals(allSetmeal)){
            List<Setmeal> list = setmealDao.findAll();
            //将数据库中查询到的套餐数据转为json格式存入redis中
            allSetmeal = JSON.toJSON(list).toString();
            jedisPool.getResource().set("allSetmeal",allSetmeal);
        }
        //将从redis中查询到的json转为java对象返回给controller
        List<Setmeal> setmealList = JSON.parseObject(allSetmeal, new TypeReference<List<Setmeal>>(){});
        return setmealList;
    }

    //根据套餐ID查询套餐详情（套餐基本信息、套餐对应的检查组信息、检查组对应的检查项信息）
    public Setmeal findById(int id) {
        //从redis中查询套餐详情数据
        String Rsetmeal = jedisPool.getResource().get("setmeal");
        //若redis中没有套餐详情数据则进行数据库查询
        if(Rsetmeal == null || "".equals(Rsetmeal) ){
            Setmeal setmeal = setmealDao.findById(id);
            //将查询到的java对象转为json后存入redis
            Rsetmeal = JSON.toJSONString(setmeal);
            jedisPool.getResource().set("setmeal",Rsetmeal);
        }
        //将json转为java对象后返回给controller
        Setmeal setmeal = JSON.parseObject(Rsetmeal,Setmeal.class);
        return setmeal;
    }

    //查询套餐预约占比数据
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    //设置套餐和检查组多对多关系，操作t_setmeal_checkgroup
    public void setSetmealAndCheckgroup(Integer setmealId,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkgroupId);
                setmealDao.setSetmealAndCheckGroup(map);
            }
        }
    }
}
